import 'dart:async';

import 'package:display_metrics/display_metrics.dart';
import 'package:flame/game.dart';
import 'package:get/get.dart';
import 'package:pmviewer/app/modules/ecg/controllers/ecgsim.dart';
import 'package:pmviewer/app/modules/ecg/views/wavecontext.dart';

import '../../../../ecgpacket/ecgpacket.pbenum.dart';
import '../views/ecgwave.dart';
import 'sinsim.dart';

class EcgController extends GetxController {
  Timer? simTimer;
  late DisplayMetricsData metrics;
  final count = 0.obs;
  var sinSim = SinSignal();
  var ecgSim = EcgSim();
  late Map<EcgChan, EcgWave> ecgRealWaves;
  late Map<EcgChan, EcgWave> ecgRecordWaves;

  @override
  void onInit() {
    super.onInit();
    print('oninit');
  }

  @override
  void onReady() {
    super.onReady();
    print('onready');

    // Timer.periodic(const Duration(milliseconds: 1000), (timer) {
    //   var ss = sinSim.sampleMany((sinSim.sr).toInt());
    //   var ss2 = ss.map((e) => (e * 120).toInt()).toList();
    //   ecgWaves.forEach((chan, w) {
    //     w.waveContext.CacheData(ss2);
    //   });
    // });

    // Timer.periodic(const Duration(milliseconds: 6000), (timer) {
    //   speed = waveSpeedNext(speed);
    //   amp = waveAmpNext(amp);
    //   ecgWaves.forEach((chan, w) {
    //     w.waveContext.setSpeed(speed);
    //     w.waveContext.setAmp(amp);
    //   });
    // });
  }

  @override
  void onClose() {
    super.onClose();
  }

  void increment() => count.value++;

  void InitWaves() {
    ecgRealWaves = <EcgChan, EcgWave>{
      EcgChan.CHAN_ECG_ELEC_POTL_I: EcgWave(
        WaveContext(
          dpiXY: metrics.inchesToLogicalPixelRatio,
          sampleRate: 180,
          valPermv: 700,
          waveName: "I",
        ),
        EcgChan.CHAN_ECG_ELEC_POTL_I,
      ),
      EcgChan.CHAN_ECG_ELEC_POTL_II: EcgWave(
        WaveContext(
          dpiXY: metrics.inchesToLogicalPixelRatio,
          sampleRate: 180,
          valPermv: 700,
          waveName: "II",
        ),
        EcgChan.CHAN_ECG_ELEC_POTL_II,
      ),
      EcgChan.CHAN_ECG_ELEC_POTL_III: EcgWave(
        WaveContext(
          dpiXY: metrics.inchesToLogicalPixelRatio,
          sampleRate: 180,
          valPermv: 700,
          waveName: "III",
        ),
        EcgChan.CHAN_ECG_ELEC_POTL_III,
      ),
      EcgChan.CHAN_ECG_ELEC_POTL_AVR: EcgWave(
        WaveContext(
          dpiXY: metrics.inchesToLogicalPixelRatio,
          sampleRate: 180,
          valPermv: 700,
          waveName: "AVR",
        ),
        EcgChan.CHAN_ECG_ELEC_POTL_AVR,
      ),
      EcgChan.CHAN_ECG_ELEC_POTL_AVL: EcgWave(
        WaveContext(
          dpiXY: metrics.inchesToLogicalPixelRatio,
          sampleRate: 180,
          valPermv: 700,
          waveName: "AVL",
        ),
        EcgChan.CHAN_ECG_ELEC_POTL_AVL,
      ),
      EcgChan.CHAN_ECG_ELEC_POTL_AVF: EcgWave(
        WaveContext(
          dpiXY: metrics.inchesToLogicalPixelRatio,
          sampleRate: 180,
          valPermv: 700,
          waveName: "AVF",
        ),
        EcgChan.CHAN_ECG_ELEC_POTL_AVF,
      ),
      EcgChan.CHAN_ECG_ELEC_POTL_V1: EcgWave(
        WaveContext(
          dpiXY: metrics.inchesToLogicalPixelRatio,
          sampleRate: 180,
          valPermv: 700,
          waveName: "V1",
        ),
        EcgChan.CHAN_ECG_ELEC_POTL_V1,
      ),
      EcgChan.CHAN_ECG_ELEC_POTL_V2: EcgWave(
        WaveContext(
          dpiXY: metrics.inchesToLogicalPixelRatio,
          sampleRate: 180,
          valPermv: 700,
          waveName: "V2",
        ),
        EcgChan.CHAN_ECG_ELEC_POTL_V2,
      ),
      EcgChan.CHAN_ECG_ELEC_POTL_V3: EcgWave(
        WaveContext(
          dpiXY: metrics.inchesToLogicalPixelRatio,
          sampleRate: 180,
          valPermv: 700,
          waveName: "V3",
        ),
        EcgChan.CHAN_ECG_ELEC_POTL_V3,
      ),
      EcgChan.CHAN_ECG_ELEC_POTL_V4: EcgWave(
        WaveContext(
          dpiXY: metrics.inchesToLogicalPixelRatio,
          sampleRate: 180,
          valPermv: 700,
          waveName: "V4",
        ),
        EcgChan.CHAN_ECG_ELEC_POTL_V4,
      ),
    };

    ecgRecordWaves = <EcgChan, EcgWave>{
      EcgChan.CHAN_ECG_ELEC_POTL_I: EcgWave(
        WaveContext(
          waveMode: WaveMode.waveModelRecord,
          dpiXY: metrics.inchesToLogicalPixelRatio,
          sampleRate: 180,
          valPermv: 700,
          waveName: "I",
        ),
        EcgChan.CHAN_ECG_ELEC_POTL_I,
      ),
      EcgChan.CHAN_ECG_ELEC_POTL_II: EcgWave(
        WaveContext(
          waveMode: WaveMode.waveModelRecord,
          dpiXY: metrics.inchesToLogicalPixelRatio,
          sampleRate: 180,
          valPermv: 700,
          waveName: "II",
        ),
        EcgChan.CHAN_ECG_ELEC_POTL_II,
      ),
      EcgChan.CHAN_ECG_ELEC_POTL_III: EcgWave(
        WaveContext(
          waveMode: WaveMode.waveModelRecord,
          dpiXY: metrics.inchesToLogicalPixelRatio,
          sampleRate: 180,
          valPermv: 700,
          waveName: "III",
        ),
        EcgChan.CHAN_ECG_ELEC_POTL_III,
      ),
      EcgChan.CHAN_ECG_ELEC_POTL_AVR: EcgWave(
        WaveContext(
          waveMode: WaveMode.waveModelRecord,
          dpiXY: metrics.inchesToLogicalPixelRatio,
          sampleRate: 180,
          valPermv: 700,
          waveName: "AVR",
        ),
        EcgChan.CHAN_ECG_ELEC_POTL_AVR,
      ),
      EcgChan.CHAN_ECG_ELEC_POTL_AVL: EcgWave(
        WaveContext(
          waveMode: WaveMode.waveModelRecord,
          dpiXY: metrics.inchesToLogicalPixelRatio,
          sampleRate: 180,
          valPermv: 700,
          waveName: "AVL",
        ),
        EcgChan.CHAN_ECG_ELEC_POTL_AVL,
      ),
      EcgChan.CHAN_ECG_ELEC_POTL_AVF: EcgWave(
        WaveContext(
          waveMode: WaveMode.waveModelRecord,
          dpiXY: metrics.inchesToLogicalPixelRatio,
          sampleRate: 180,
          valPermv: 700,
          waveName: "AVF",
        ),
        EcgChan.CHAN_ECG_ELEC_POTL_AVF,
      ),
      EcgChan.CHAN_ECG_ELEC_POTL_V1: EcgWave(
        WaveContext(
          waveMode: WaveMode.waveModelRecord,
          dpiXY: metrics.inchesToLogicalPixelRatio,
          sampleRate: 180,
          valPermv: 700,
          waveName: "V1",
        ),
        EcgChan.CHAN_ECG_ELEC_POTL_V1,
      ),
      EcgChan.CHAN_ECG_ELEC_POTL_V2: EcgWave(
        WaveContext(
          waveMode: WaveMode.waveModelRecord,
          dpiXY: metrics.inchesToLogicalPixelRatio,
          sampleRate: 180,
          valPermv: 700,
          waveName: "V2",
        ),
        EcgChan.CHAN_ECG_ELEC_POTL_V2,
      ),
      EcgChan.CHAN_ECG_ELEC_POTL_V3: EcgWave(
        WaveContext(
          waveMode: WaveMode.waveModelRecord,
          dpiXY: metrics.inchesToLogicalPixelRatio,
          sampleRate: 180,
          valPermv: 700,
          waveName: "V3",
        ),
        EcgChan.CHAN_ECG_ELEC_POTL_V3,
      ),
      EcgChan.CHAN_ECG_ELEC_POTL_V4: EcgWave(
        WaveContext(
          waveMode: WaveMode.waveModelRecord,
          dpiXY: metrics.inchesToLogicalPixelRatio,
          sampleRate: 180,
          valPermv: 700,
          waveName: "V4",
        ),
        EcgChan.CHAN_ECG_ELEC_POTL_V4,
      ),
    };
    // 模拟数据
    simTimer ??= Timer.periodic(const Duration(milliseconds: 200), (timer) {
      var ss = ecgSim.sampleMany((ecgSim.sr * 0.2).toInt());
      var ss2 = ss.map((e) => (e).toInt()).toList();
      ecgRealWaves.forEach((chan, w) {
        w.waveContext.CacheData(ss2);
      });
      ecgRecordWaves.forEach((chan, w) {
        w.waveContext.CacheData(ss2);
      });
    });
  }

  void relayoutEcgRealWaves(Vector2 size) {
    EcgController controller = Get.find();
    var wpos = Vector2(0, 0);
    var wsize = Vector2(size.x,
        controller.ecgRecordWaves.values.first.waveContext.waveHeight());
    ecgRealWaves.forEach((chan, wave) {
      wave.setPositionAndSize(wpos, wsize);
      wpos.y += controller.ecgRecordWaves.values.first.waveContext.waveHeight();
    });
  }

  void relayoutEcgRecordWaves(Vector2 size) {
    EcgController controller = Get.find();
    var wpos = Vector2(0, 0);
    var wsize = Vector2(size.x,
        controller.ecgRecordWaves.values.first.waveContext.waveHeight());
    ecgRecordWaves.forEach((chan, wave) {
      wave.setPositionAndSize(wpos, wsize);
      wpos.y += controller.ecgRecordWaves.values.first.waveContext.waveHeight();
    });
  }
}
